package constant.milk.periodapp.dialog;


import java.util.ArrayList;
import java.util.Calendar;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import constant.milk.periodapp.R;
import constant.milk.periodapp.util.MilkUtil;

public class MilkDayDialog extends Dialog implements View.OnClickListener
{
	private View contentView;
	
	private TextView preTextView;
	private TextView nextTextView;
	private TextView yearTextView;
	
	private TextView dialogDay1_0TextView;
	private TextView dialogDay1_1TextView;
	private TextView dialogDay1_2TextView;
	private TextView dialogDay1_3TextView;
	private TextView dialogDay1_4TextView;
	private TextView dialogDay1_5TextView;
	private TextView dialogDay1_6TextView;
	
	private TextView dialogDay2_0TextView;
	private TextView dialogDay2_1TextView;
	private TextView dialogDay2_2TextView;
	private TextView dialogDay2_3TextView;
	private TextView dialogDay2_4TextView;
	private TextView dialogDay2_5TextView;
	private TextView dialogDay2_6TextView;
	
	private TextView dialogDay3_0TextView;
	private TextView dialogDay3_1TextView;
	private TextView dialogDay3_2TextView;
	private TextView dialogDay3_3TextView;
	private TextView dialogDay3_4TextView;
	private TextView dialogDay3_5TextView;
	private TextView dialogDay3_6TextView;

	private TextView dialogDay4_0TextView;
	private TextView dialogDay4_1TextView;
	private TextView dialogDay4_2TextView;
	private TextView dialogDay4_3TextView;
	private TextView dialogDay4_4TextView;
	private TextView dialogDay4_5TextView;
	private TextView dialogDay4_6TextView;
	
	private TextView dialogDay5_0TextView;
	private TextView dialogDay5_1TextView;
	private TextView dialogDay5_2TextView;
	private TextView dialogDay5_3TextView;
	private TextView dialogDay5_4TextView;
	private TextView dialogDay5_5TextView;
	private TextView dialogDay5_6TextView;
	
	private TextView dialogDay6_0TextView;
	private TextView dialogDay6_1TextView;
	private TextView dialogDay6_2TextView;
	private TextView dialogDay6_3TextView;
	private TextView dialogDay6_4TextView;
	private TextView dialogDay6_5TextView;
	private TextView dialogDay6_6TextView;
	
	private ArrayList<TextView> dayList = new ArrayList<TextView>();
	
	private DateCallBank callbank = null;
	
	private Calendar cal;
	
	private int setYear;
	private int setMonth;
	private int setDay;
	
	private int toYear;
	private int toMonth;
	private int toDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_day);
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		contentView = findViewById(R.id.dialogDayContentView);
		
		preTextView = (TextView) findViewById(R.id.dialogDayPreTextView);
		nextTextView = (TextView) findViewById(R.id.dialogDayNextTextView);
		yearTextView = (TextView) findViewById(R.id.dialogDayDateTextView);
		
		dialogDay1_0TextView = (TextView) findViewById(R.id.dialogDay1_0TextView);
		dialogDay1_1TextView = (TextView) findViewById(R.id.dialogDay1_1TextView);
		dialogDay1_2TextView = (TextView) findViewById(R.id.dialogDay1_2TextView);
		dialogDay1_3TextView = (TextView) findViewById(R.id.dialogDay1_3TextView);
		dialogDay1_4TextView = (TextView) findViewById(R.id.dialogDay1_4TextView);
		dialogDay1_5TextView = (TextView) findViewById(R.id.dialogDay1_5TextView);
		dialogDay1_6TextView = (TextView) findViewById(R.id.dialogDay1_6TextView);
		
		dialogDay2_0TextView = (TextView) findViewById(R.id.dialogDay2_0TextView);
		dialogDay2_1TextView = (TextView) findViewById(R.id.dialogDay2_1TextView);
		dialogDay2_2TextView = (TextView) findViewById(R.id.dialogDay2_2TextView);
		dialogDay2_3TextView = (TextView) findViewById(R.id.dialogDay2_3TextView);
		dialogDay2_4TextView = (TextView) findViewById(R.id.dialogDay2_4TextView);
		dialogDay2_5TextView = (TextView) findViewById(R.id.dialogDay2_5TextView);
		dialogDay2_6TextView = (TextView) findViewById(R.id.dialogDay2_6TextView);
		
		dialogDay3_0TextView = (TextView) findViewById(R.id.dialogDay3_0TextView);
		dialogDay3_1TextView = (TextView) findViewById(R.id.dialogDay3_1TextView);
		dialogDay3_2TextView = (TextView) findViewById(R.id.dialogDay3_2TextView);
		dialogDay3_3TextView = (TextView) findViewById(R.id.dialogDay3_3TextView);
		dialogDay3_4TextView = (TextView) findViewById(R.id.dialogDay3_4TextView);
		dialogDay3_5TextView = (TextView) findViewById(R.id.dialogDay3_5TextView);
		dialogDay3_6TextView = (TextView) findViewById(R.id.dialogDay3_6TextView);
		
		dialogDay4_0TextView = (TextView) findViewById(R.id.dialogDay4_0TextView);
		dialogDay4_1TextView = (TextView) findViewById(R.id.dialogDay4_1TextView);
		dialogDay4_2TextView = (TextView) findViewById(R.id.dialogDay4_2TextView);
		dialogDay4_3TextView = (TextView) findViewById(R.id.dialogDay4_3TextView);
		dialogDay4_4TextView = (TextView) findViewById(R.id.dialogDay4_4TextView);
		dialogDay4_5TextView = (TextView) findViewById(R.id.dialogDay4_5TextView);
		dialogDay4_6TextView = (TextView) findViewById(R.id.dialogDay4_6TextView);
		
		dialogDay5_0TextView = (TextView) findViewById(R.id.dialogDay5_0TextView);
		dialogDay5_1TextView = (TextView) findViewById(R.id.dialogDay5_1TextView);
		dialogDay5_2TextView = (TextView) findViewById(R.id.dialogDay5_2TextView);
		dialogDay5_3TextView = (TextView) findViewById(R.id.dialogDay5_3TextView);
		dialogDay5_4TextView = (TextView) findViewById(R.id.dialogDay5_4TextView);
		dialogDay5_5TextView = (TextView) findViewById(R.id.dialogDay5_5TextView);
		dialogDay5_6TextView = (TextView) findViewById(R.id.dialogDay5_6TextView);
		
		dialogDay6_0TextView = (TextView) findViewById(R.id.dialogDay6_0TextView);
		dialogDay6_1TextView = (TextView) findViewById(R.id.dialogDay6_1TextView);
		dialogDay6_2TextView = (TextView) findViewById(R.id.dialogDay6_2TextView);
		dialogDay6_3TextView = (TextView) findViewById(R.id.dialogDay6_3TextView);
		dialogDay6_4TextView = (TextView) findViewById(R.id.dialogDay6_4TextView);
		dialogDay6_5TextView = (TextView) findViewById(R.id.dialogDay6_5TextView);
		dialogDay6_6TextView = (TextView) findViewById(R.id.dialogDay6_6TextView);
	}
	
	private void setOnClick()
	{
		contentView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{	
				dismiss();
			}
		});
		
		preTextView.setOnClickListener(this);
		nextTextView.setOnClickListener(this);
		
		dialogDay1_0TextView.setOnClickListener(this);
		dialogDay1_1TextView.setOnClickListener(this);
		dialogDay1_2TextView.setOnClickListener(this);
		dialogDay1_3TextView.setOnClickListener(this);
		dialogDay1_4TextView.setOnClickListener(this);
		dialogDay1_5TextView.setOnClickListener(this);
		dialogDay1_6TextView.setOnClickListener(this);
		
		dialogDay2_0TextView.setOnClickListener(this);
		dialogDay2_1TextView.setOnClickListener(this);
		dialogDay2_2TextView.setOnClickListener(this);
		dialogDay2_3TextView.setOnClickListener(this);
		dialogDay2_4TextView.setOnClickListener(this);
		dialogDay2_5TextView.setOnClickListener(this);
		dialogDay2_6TextView.setOnClickListener(this);
		
		dialogDay3_0TextView.setOnClickListener(this);
		dialogDay3_1TextView.setOnClickListener(this);
		dialogDay3_2TextView.setOnClickListener(this);
		dialogDay3_3TextView.setOnClickListener(this);
		dialogDay3_4TextView.setOnClickListener(this);
		dialogDay3_5TextView.setOnClickListener(this);
		dialogDay3_6TextView.setOnClickListener(this);
		
		dialogDay4_0TextView.setOnClickListener(this);
		dialogDay4_1TextView.setOnClickListener(this);
		dialogDay4_2TextView.setOnClickListener(this);
		dialogDay4_3TextView.setOnClickListener(this);
		dialogDay4_4TextView.setOnClickListener(this);
		dialogDay4_5TextView.setOnClickListener(this);
		dialogDay4_6TextView.setOnClickListener(this);
		
		dialogDay5_0TextView.setOnClickListener(this);
		dialogDay5_1TextView.setOnClickListener(this);
		dialogDay5_2TextView.setOnClickListener(this);
		dialogDay5_3TextView.setOnClickListener(this);
		dialogDay5_4TextView.setOnClickListener(this);
		dialogDay5_5TextView.setOnClickListener(this);
		dialogDay5_6TextView.setOnClickListener(this);
		
		dialogDay6_0TextView.setOnClickListener(this);
		dialogDay6_1TextView.setOnClickListener(this);
		dialogDay6_2TextView.setOnClickListener(this);
		dialogDay6_3TextView.setOnClickListener(this);
		dialogDay6_4TextView.setOnClickListener(this);
		dialogDay6_5TextView.setOnClickListener(this);
		dialogDay6_6TextView.setOnClickListener(this);
	}
	
	private void setData()
	{
		setTextList();
		
		dialogDay1_0TextView.setTextColor(Color.RED);
		dialogDay1_6TextView.setTextColor(Color.BLUE);
		
		dialogDay2_0TextView.setTextColor(Color.RED);
		dialogDay2_6TextView.setTextColor(Color.BLUE);
		
		dialogDay3_0TextView.setTextColor(Color.RED);
		dialogDay3_6TextView.setTextColor(Color.BLUE);
		
		dialogDay4_0TextView.setTextColor(Color.RED);
		dialogDay4_6TextView.setTextColor(Color.BLUE);
		
		dialogDay5_0TextView.setTextColor(Color.RED);
		dialogDay5_6TextView.setTextColor(Color.BLUE);
		
		dialogDay6_0TextView.setTextColor(Color.RED);
		dialogDay6_6TextView.setTextColor(Color.BLUE);
		
		setYear = cal.get(Calendar.YEAR);
		setMonth = cal.get(Calendar.MONTH);
		setDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setCalendar(setYear, setMonth);
	}
	
	private void setCalendar(int year, int month) 
	{
		for (int i = 0; i < dayList.size(); i++)
		{
			TextView tv = (TextView) dayList.get(i);
			tv.setBackgroundColor(0);
			tv.setText("");
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		yearTextView.setText(MilkUtil.getDate(year, month+1));

		int getDay = calendar.get(Calendar.DAY_OF_WEEK);
		
		int j = 1;

		for (int i = getDay - 1; i < calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
		{
			TextView tv = dayList.get(i);
			tv.setTag(j);
			
			if (j == toDay) 
			{
				if ((setYear == toYear) && (setMonth == toMonth)) 
				{
					tv.setBackgroundResource(R.drawable.bg_gray);
				}
				
				tv.setText(j + "");
			}
			else 
			{
				tv.setText(j + "");
			}
			
			j++;
		}
	}

	public MilkDayDialog(Context context, DateCallBank callbank, int year, int month, int day)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.callbank = callbank;
		
		cal = Calendar.getInstance();
		
		toYear = year;
		toMonth = month;
		toDay = day;
		
		cal.set(year, month, day);
	}
	
	private void setTextList()
	{
		dayList.add(dialogDay1_0TextView);
		dayList.add(dialogDay1_1TextView);
		dayList.add(dialogDay1_2TextView);
		dayList.add(dialogDay1_3TextView);
		dayList.add(dialogDay1_4TextView);
		dayList.add(dialogDay1_5TextView);
		dayList.add(dialogDay1_6TextView);
		
		dayList.add(dialogDay2_0TextView);
		dayList.add(dialogDay2_1TextView);
		dayList.add(dialogDay2_2TextView);
		dayList.add(dialogDay2_3TextView);
		dayList.add(dialogDay2_4TextView);
		dayList.add(dialogDay2_5TextView);
		dayList.add(dialogDay2_6TextView);
		
		dayList.add(dialogDay3_0TextView);
		dayList.add(dialogDay3_1TextView);
		dayList.add(dialogDay3_2TextView);
		dayList.add(dialogDay3_3TextView);
		dayList.add(dialogDay3_4TextView);
		dayList.add(dialogDay3_5TextView);
		dayList.add(dialogDay3_6TextView);
		
		dayList.add(dialogDay4_0TextView);
		dayList.add(dialogDay4_1TextView);
		dayList.add(dialogDay4_2TextView);
		dayList.add(dialogDay4_3TextView);
		dayList.add(dialogDay4_4TextView);
		dayList.add(dialogDay4_5TextView);
		dayList.add(dialogDay4_6TextView);
		
		dayList.add(dialogDay5_0TextView);
		dayList.add(dialogDay5_1TextView);
		dayList.add(dialogDay5_2TextView);
		dayList.add(dialogDay5_3TextView);
		dayList.add(dialogDay5_4TextView);
		dayList.add(dialogDay5_5TextView);
		dayList.add(dialogDay5_6TextView);
	
		dayList.add(dialogDay6_0TextView);
		dayList.add(dialogDay6_1TextView);
		dayList.add(dialogDay6_2TextView);
		dayList.add(dialogDay6_3TextView);
		dayList.add(dialogDay6_4TextView);
		dayList.add(dialogDay6_5TextView);
		dayList.add(dialogDay6_6TextView);
	}

	@Override
	public void onClick(View v)
	{
		if (v.getId() == R.id.dialogDayPreTextView)
		{
			cal.add(Calendar.MONTH, -1);
			
			setYear = cal.get(Calendar.YEAR);
			setMonth = cal.get(Calendar.MONTH);
			setDay = cal.get(Calendar.DAY_OF_MONTH);
			
			setCalendar(setYear, setMonth);
		}
		else if (v.getId() == R.id.dialogDayNextTextView)
		{
			cal.add(Calendar.MONTH, 1);
			
			setYear = cal.get(Calendar.YEAR);
			setMonth = cal.get(Calendar.MONTH);
			setDay = cal.get(Calendar.DAY_OF_MONTH);
			
			setCalendar(setYear, setMonth);
		}
		else
		{
			if (v.getTag() != null && (Integer) v.getTag() > 0)
			{
				setDay = (int) v.getTag();
				
				callbank.getDate(setYear, setMonth, setDay);
				
				dismiss();
			}
		}
	}
	
	public interface DateCallBank
	{
		public abstract void getDate(int year, int month, int day);
	}
}
